import shop from '@/assets/shopAPI.js'
export default {
    namespaced: true,
    state: {
        list: []
    },
    mutations: {
        setList(state, payload) {
            state.list = payload
        },
        // 添加到购物车时，库存减1
        decProductInventory(state, { id }) {
            const product = state.list.find(product => product.id === id)
            product.inventory--
        },
        // 从购物车删除时，库存加1
        incProductInventory(state, { id }) {
            const product = state.list.find(product => product.id === id)
            product.inventory++
        },
    },
    actions: {
        getProList({ commit }) {
            setTimeout(() => {
                shop.getProducts(products => {
                    commit('setList', products)
                })
            }, 300);
        }
    },
    getters: {}
}